﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Windows.Threading;

namespace SoundServant
{
    /// <summary>
    /// Interaction logic for LoadWindow.xaml
    /// </summary>
    public partial class KeyboardEntryWindow : SSWindow
    {
        bool caps = false;
        bool password = false;
        private string origText;
        private string text;
        public string Field
        {
            get
            {
                return text;
            }
        }

        public KeyboardEntryWindow(string _text, string _fieldName)
        {
            InitializeComponent();

            origText = _text;
            text = _text;
            TextLabel.Content = text;
            FieldNameLabel.Content = _fieldName;
        }

        public KeyboardEntryWindow(string _field, string _fieldName, bool _password)
        {
            InitializeComponent();
            origText = _field;
            text = _field;
            password = _password;
            Draw();
            FieldNameLabel.Content = _fieldName;
        }

        void Draw()
        {
            if (!password)
            {
                TextLabel.Content = text;
            }
            else
            {
                string textToDisplay = "";
                for (int i = 0; i < text.Length; i++)
                {
                    textToDisplay += "*";
                }

                TextLabel.Content = textToDisplay;
            }
        }

        private void Key_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.Red;
        }
        private void Key_MouseEnter(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed)
            {
                Label label = (Label)sender;
                label.Foreground = Brushes.Red;
            }
        }

        private void Key_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.Black;
            text += label.Content.ToString();
            Draw();
            if (caps) ToggleCaps();
        }
        private void Key_MouseLeave(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.Black;
        }

        private void CharKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SteelBlue;
            text += label.Content.ToString();
            Draw();
        }
        private void CharKey_MouseLeave(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SteelBlue;
        }

        private void NumberKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SteelBlue;
            text += label.Content.ToString();
            Draw();
        }
        private void NumberKey_MouseLeave(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SteelBlue;
        }

        private void BackSpaceKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SeaGreen;
            if (text.Length > 0) text = text.Substring(0, text.Length - 1);
            Draw();
        }
        private void CapsKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SeaGreen;
            ToggleCaps();
        }
        private void SpaceKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SeaGreen;
            text += " ";
            Draw();
        }
        private void ClearKeyLabel_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SeaGreen;
            text = "";
            Draw();
        }

        private void Special_MouseLeave(object sender, MouseEventArgs e)
        {
            Label label = (Label)sender;
            label.Foreground = Brushes.SeaGreen;
        }

        private void ToggleCaps()
        {
            if (caps)
            {
                AKeyLabel.Content = "a";
                BKeyLabel.Content = "b";
                CKeyLabel.Content = "c";
                DKeyLabel.Content = "d";
                EKeyLabel.Content = "e";
                FKeyLabel.Content = "f";
                GKeyLabel.Content = "g";
                HKeyLabel.Content = "h";
                IKeyLabel.Content = "i";
                JKeyLabel.Content = "j";
                KKeyLabel.Content = "k";
                LKeyLabel.Content = "l";
                MKeyLabel.Content = "m";
                NKeyLabel.Content = "n";
                OKeyLabel.Content = "o";
                PKeyLabel.Content = "p";
                QKeyLabel.Content = "q";
                RKeyLabel.Content = "r";
                SKeyLabel.Content = "s";
                TKeyLabel.Content = "t";
                UKeyLabel.Content = "u";
                VKeyLabel.Content = "v";
                WKeyLabel.Content = "w";
                XKeyLabel.Content = "x";
                YKeyLabel.Content = "y";
                ZKeyLabel.Content = "z";
                Caps1KeyLabel.FontWeight = FontWeights.Normal;
                Caps2KeyLabel.FontWeight = FontWeights.Normal;
                caps = false;
            }
            else
            {
                AKeyLabel.Content = "A";
                BKeyLabel.Content = "B";
                CKeyLabel.Content = "C";
                DKeyLabel.Content = "D";
                EKeyLabel.Content = "E";
                FKeyLabel.Content = "F";
                GKeyLabel.Content = "G";
                HKeyLabel.Content = "H";
                IKeyLabel.Content = "I";
                JKeyLabel.Content = "J";
                KKeyLabel.Content = "K";
                LKeyLabel.Content = "L";
                MKeyLabel.Content = "M";
                NKeyLabel.Content = "N";
                OKeyLabel.Content = "O";
                PKeyLabel.Content = "P";
                QKeyLabel.Content = "Q";
                RKeyLabel.Content = "R";
                SKeyLabel.Content = "S";
                TKeyLabel.Content = "T";
                UKeyLabel.Content = "U";
                VKeyLabel.Content = "V";
                WKeyLabel.Content = "W";
                XKeyLabel.Content = "X";
                YKeyLabel.Content = "Y";
                ZKeyLabel.Content = "Z";
                Caps1KeyLabel.FontWeight = FontWeights.Bold;
                Caps2KeyLabel.FontWeight = FontWeights.Bold;
                caps = true;
            }
        }

        public static string ShowKeyboard(string _field, string _fieldName)
        {
            KeyboardEntryWindow keyboardEntryWindow = new KeyboardEntryWindow(_field, _fieldName);
            keyboardEntryWindow.ShowDialog();
            return keyboardEntryWindow.Field;
        }

        public static string ShowKeyboard(string _field, string _fieldName, bool _password)
        {
            KeyboardEntryWindow keyboardEntryWindow = new KeyboardEntryWindow(_field, _fieldName, _password);
            keyboardEntryWindow.ShowDialog();
            return keyboardEntryWindow.Field;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Closing -= SSWindow_Closing;
            TextLabel.Content = origText;
            this.Close();
        }

        private void SSWindow_Closing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            this.Closing -= SSWindow_Closing;
            this.Close();
        }

        private void SSWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}
